//
//  ViewController.m
//  20202
//
//  Created by student on 16/3/16.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textview;
@property (copy,nonatomic)NSMutableArray *marr;
@property (copy,nonatomic)NSString *mode;

@end

@implementation ViewController

- (NSMutableArray *)marr
{
    if (_marr == nil)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self plistPath]])
        {
            _marr = [NSMutableArray arrayWithContentsOfFile:[self plistPath]];
        }
        else
        {
            _marr = [NSMutableArray array];
        }
    }
    return _marr;
}
- (NSString *)plistPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"time.plist"];
    //    NSLog(@"存储位置:%@",filePath);
    return filePath;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(willdisactive) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didback) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(willfore) name:UIApplicationWillEnterForegroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didact) name:UIApplicationDidBecomeActiveNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setcolor) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)setcolor
{
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    NSString *red = [d valueForKey:@"redslider"];
    NSString *green = [d valueForKey:@"greenslider"];
    NSString *blue = [d valueForKey:@"blueslider"];
    NSLog(@"red = %@",red);
    NSLog(@"green = %@",green);
    NSLog(@"blue = %@",blue);
    self.view.backgroundColor = [UIColor colorWithRed:[red floatValue]/255 green:[green floatValue]/255 blue:[blue floatValue]/255 alpha:1];
}
- (void)dealloc
{
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)willdisactive
{
    self.mode = @"将取消活动";
    [self printinfo];
}
- (void)didback
{
    self.mode = @"已经进入后台";
    [self printinfo];
}
- (void)willfore
{
    self.mode = @"将进入前台";
    [self printinfo];
}
- (void)didact
{
    self.mode = @"已经激活";
    [self printinfo];
}

//* 将取消活动
//* 已经进入后台
//* 将进入前台
//* 已经激活

- (void)printinfo
{
    NSMutableString *mstr = [NSMutableString string];
    for (NSString *tmp in self.marr)
    {
        [mstr appendString:tmp];
    }
   

    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"HH:mm:ss";
    NSString *timestr = [formatter stringFromDate:date];
    NSLog(@"timestr = %@",timestr);
    NSString *str = [[NSString alloc]initWithString:timestr];
    [mstr appendString:[NSString stringWithFormat:@"%@ %@ \n",str,self.mode]];
//    [mstr appendString:@"\n"];
    [self.marr addObject:[NSString stringWithFormat:@"%@ %@ \n",str,self.mode]];
    [self.marr writeToFile:[self plistPath] atomically:YES];
    self.textview.text = mstr;

    
}



@end
